<%@ page language="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<link type="text/css" rel="stylesheet" href="${ctx }/resources/js/jstree/themes/default/style.min.css"/>
<script type="text/javascript" src="${ctx }/resources/js/jstree/jstree.js"></script>
<%--<script type="text/javascript" src="${ctx }/resources/js/jstree/jstree.checkbox.js"></script>--%>
<script type="text/javascript" src="${ctx }/resources/js/underscore/underscore-min.js"></script>

<div class="page-header">
	<h1>
		权限设置-<small>[${roleEntity.name }]</small>
	</h1>
</div>
<div class="row" style="margin-top:5px;">
	<div class="col-xs-12">
		<form id="permissionForm" name="permissionForm" class="form-horizontal" role="form" method="post">
		<input type="hidden" id="pageNum" name="pageNum" value="${page.pageNum }">
		<input type="hidden" id="pageSize" name="pageSize" value="${page.pageSize }">
		<input type="hidden" id="orderByColumn" name="orderByColumn" value="${page.orderByColumn }">
		<input type="hidden" id="orderByType" name="orderByType" value="${page.orderByType }">
		<input type="hidden" id="id" name="id" value="${roleEntity.id }">
		   <!-- 资源树 -->
		   <div class="form-group">
		      <label class="control-label col-sm-1 no-padding-right" for="description">资源</label>
		      <div class="col-sm-10">
		         <div id="tree"></div>
		      </div>
		   </div>
		</form>
		<div class="hr hr-dotted"></div>
	</div>
</div>
<div class="center">
	<button id="btnAdd" type="button" class="btn btn-success btn-sm">
	  	<i class="fa fa-user-plus"></i>&nbsp;
		保存
	</button>
		<button id="btnBack" type="button" class="btn btn-info btn-sm">
		<i class="fa fa-trash-o"></i>&nbsp;返回
	</button>
</div>

<script type="text/javascript">

    $(function() {
        initButtonClick();
        initResourceTree();
    });


    var resoure = {
        ids : [],
        /**
         * 递归遍历节点，获取该节点的所有父节点
         * @param {Object} treeObj  jstree对象
         * @param {Object} nodeObj  jstree node节点对象
         */
        getParents : function (treeObj, nodeObj) {
            var parentId = treeObj.get_parent(nodeObj);
            if (parentId != "#") {
                resoure.ids.push(parentId);
                resoure.getParents(treeObj, treeObj.get_node(parentId));
            }
		}
    };


    /**
     *  初始化绑定按钮事件
     */
    function initButtonClick() {
        $("#btnAdd").click(function(){
            commitPerm();
        });


        $("#btnBack").click(function(){
            movic.common.loadPage('/role/listUI.html?page=${page.pageNum }&rows=${page.pageSize }&sidx=${page.orderByColumn }&sord=${page.orderByType }')
        });
    }


    /**
	 * 初始化资源树
     */
    function initResourceTree() {
        $('#tree').jstree({
            "core" : {
                'data' : {
                    "url" : sys.rootPath + "/resource/withoutAuth/resourceTree.html?roleId=" + $("#id").val(),
                    "dataType" : "json"
                },
                "themes" : {
                    "responsive" : true
                },
                "multiple" : true,
                "animation" : 200,
                "dblclick_toggle" : true,
                "expand_selected_onload" : true
            },
            "checkbox" : {
                "keep_selected_style" : false,
                "three_state" : true,
                "cascade" : "up"
            },
            "plugins" : ["checkbox"]
        });
    }

    /**
	 * 角色授权提交表单
     */
    function commitPerm() {
        resoure.ids = [];
        var tree = $('#tree').jstree();
        //获取所有选中节点id
        var selectedIds = tree.get_checked();
        //获取所有选中节点
        var selected = tree.get_checked(true);
        //遍历节点，获取选中节点的所有父节点
        for (var i = 0; i < selected.length; i++) {
            resoure.getParents(tree, selected[i]);
        }
        var index;
        $.ajax({
            type : "POST",
            url : sys.rootPath + '/role/permission.html',
            data : {
                "roleId" : $("#id").val(),
                "resourceIds" : _.union(resoure.ids, selectedIds).join(',')
            },
            dataType : "json",
            beforeSend : function() {
                index = layer.load();
            },
            success : function(result) {
                layer.close(index);
                if (result.code === 200) {
                    layer.msg(result.message, {
                        icon : 1
                    });
                    movic.common.loadPage('/role/listUI.html' + '?page=' + $("#pageNum").val() + '&rows=' + $("#pageSize").val() + '&sidx=' + $("#orderByColumn").val() + '&sord=' + $("#orderByType").val());
                } else {
                    layer.msg(result.message, {
                        icon : 5
                    });
                }
            },
            error : function(errorMsg) {
                layer.close(index);
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 2
                });
            }
        });
	}


</script>